package com.api.signature.auto.config;

import com.signature.auto.common.filter.RepeatableFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Jerry
 * @date 2024-01-24 17:50
 * Filter配置
 */
@Configuration
public class FilterConfig {
    /**
     * 解决getReader() has already been called for this request 问题
     * 原因：getReader() 是流只能读一次，读了就变成-1 ，getInputStream()也一样
     * <p>
     * 解决原理装饰模式：
     * <p>
     * 继承HttpServletRequestWrapper重写getInputStream()和getReader()   然后用Filter 把继承了HttpServletRequestWrapper往下传，在这一线程中需要用getInputStream()/getReader()都去新的类中取
     * <p>
     * RepeatableFilter 看详情信息
     *
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RepeatableFilter());
        registration.addUrlPatterns("/*");
        registration.setName("repeatableFilter");
        registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
        return registration;
    }
}
